package com.yisu.job.service.impl;


import com.yisu.expressway.ewf.entity.OrderMain;
import com.yisu.expressway.ewf.entity.OrderSub;
import com.yisu.job.mapper.OrderMainMapper;
import com.yisu.job.mapper.OrderSubMapper;
import com.yisu.job.mapper.SpecificationGoodsMapper;
import com.yisu.job.models.GoodsInfoRequest;
import com.yisu.job.service.IOrderMainService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * desc:
 * email:zhaojie07@dianyadian.com
 * Created by Administrator on 2016/12/26.
 */
@Service
public class OrderMainServiceImpl implements IOrderMainService {

    @Resource
    private OrderMainMapper orderMainMapper;
    @Resource
    private OrderSubMapper orderSubMapper;
    @Resource
    private SpecificationGoodsMapper specificationGoodsMapper;

    /**
     * 通过订单号取消订单
     *
     * @param cancelTimeLimit
     * @return
     */
    @Override
    public boolean cancelOrder(Integer cancelTimeLimit) {
        try {
            if (cancelTimeLimit == null || cancelTimeLimit == 0)
                cancelTimeLimit = 30;
            List<OrderMain> orderMains = orderMainMapper.cancelOrderList(cancelTimeLimit);
            if (orderMains.size() > 0) {
                for (OrderMain orderMain : orderMains) {
                    orderMainMapper.updateOrderStatus(orderMain);
                    try {
                        List<OrderSub> orderSubList = orderSubMapper.getOrderSubListByOrderId(orderMain.getOrderId());
                        if (!CollectionUtils.isEmpty(orderSubList)) {
                            for (OrderSub orderSub : orderSubList) {
                                //订单取消，补充库存
                                GoodsInfoRequest goodsInfoRequest = new GoodsInfoRequest();
                                goodsInfoRequest.setGoodsNo(orderSub.getGoodsNo());
                                goodsInfoRequest.setSpecificationNo(orderSub.getSpecificationNo());
                                goodsInfoRequest.setCategoryCode(String.valueOf(orderSub.getCategoryCode()));
                                goodsInfoRequest.setSubtractInventoryNum(orderSub.getBuyNumber());
                                specificationGoodsMapper.addInventoryNum(goodsInfoRequest);
                            }
                        }
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
                return true;
            }
        } catch (Exception ex) {
            return false;
        }
        return false;
    }
}
